package com.example.demo3.controller;



import com.example.demo3.mapper.takenotesmapper;
import com.example.demo3.mapper.usersmapper;
import com.example.demo3.pojo.takenotes;
import com.example.demo3.pojo.takenotes1;
import com.example.demo3.pojo.users;
import com.example.demo3.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class userscontroller {

       @Autowired
       usersmapper usersmapper;
       @Autowired
       takenotesmapper takenotesmapper;
       //添加用户
    @RequestMapping("/add")
    public ResponseResult<String> add(String name,double yue){
        int add = usersmapper.add(name, yue);
        if (add>0){
            int i = takenotesmapper.add2(name, yue);
            return ResponseResult.success("成功插入一条用户信息名称为："+name+"额度为："+yue);
        }
       return ResponseResult.error();
    }
       //查詢餘額
       @RequestMapping("/list")
       public List<users> select(String name){
              List<users> users = usersmapper.selectList1(name);
              return users;
       }
       //消費一百元
    /**
     *
     * @param name 用户名
     * @param yue  消费金额
     *      int  num 随机生成的订单
     * @return
     */
    @RequestMapping("/xiaofei")
    public ResponseResult<String> update(String name,double yue){
        int a=1000000000;
        int b=2000000000;
        int num = (int) (a + Math.random() *(b-a+1));
        int i = usersmapper.update1(name, yue);
        double v = usersmapper.select2(name);
        takenotesmapper.add(name,v,yue,"jd"+num);
        return  ResponseResult.success("您已消费"+yue+"\t还剩余额"+v+"\n您的订单编号为：jd"+num);
    }

//    用户退款
    /**
     *
     * @param name   用户名
     * @param goods   订单号
     * @return
     */
    @RequestMapping("/tuik")
    public ResponseResult<String> tuik(String name,String goods){
        String selectgoods = null;
        int i;
       try {
           selectgoods = takenotesmapper.selectgoods(goods);
             i = usersmapper.update2(name,selectgoods);
       }catch (Exception e){
           return ResponseResult.error("您的订单已办理过退款请，不要重复");
       }

        if(i>0){
            int selectid = takenotesmapper.selectid(goods);
            takenotesmapper.deleteById(selectid);
            return ResponseResult.success("您的订单："+goods+"\n已退款");
        }else return ResponseResult.error();

    }
    //查询用户金额变动明细
    @RequestMapping("/lis")
    public ResponseResult<List> select2(String name){
        List<takenotes1> takenotes = takenotesmapper.selectList2(name);
        if(takenotes.isEmpty()){return ResponseResult.error("请输入正确的用户名");

        }
        return ResponseResult.success(takenotes);
    }



}
